package com.dao;

import com.entity.Blog;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/6/16 9:15
 **/
public class BlogDaoImpl extends BaseDao implements BlogDao {
	@Override
	public List<Blog> findAll() {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			//获取连接对象
			 conn = getConnection();
			//编写sql
			String sql = "Select * from blog order by id desc";
			//创建执行对象
			 pstm = conn.prepareStatement(sql);
			//执行
			 rs = pstm.executeQuery();
			//处理
			List<Blog> list = new ArrayList<Blog>();
			while(rs.next()){
				Blog blog = new Blog(rs.getInt("id"),
						rs.getInt("uid"),
						rs.getString("content"),
						rs.getString("images"),
						rs.getString("time"),
						rs.getInt("state"),
						rs.getInt("fromBid"));
				list.add(blog); //将从数据库中获取的数据，循环添加到==》集合
			}
			return list;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			//关闭
			closeAll(rs,pstm,conn);
		}
		return null;
	}

	@Override
	public Blog findById(Integer id) {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			//获取连接对象
			conn = getConnection();
			//编写sql
			String sql = "Select * from blog where id=?";
			//创建执行对象
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1, id);
			//执行
			rs = pstm.executeQuery();
			//处理
			Blog blog = null;
			while(rs.next()){
				 blog = new Blog(rs.getInt("id"),
						rs.getInt("uid"),
						rs.getString("content"),
						rs.getString("images"),
						rs.getString("time"),
						rs.getInt("state"),
						rs.getInt("fromBid"));
			}
			return blog;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			//关闭
			closeAll(rs,pstm,conn);
		}
		return null;
	}

	@Override
	public int add(Blog blog) {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			//获取连接对象
			conn = getConnection();
			//编写sql
			String sql = "Insert into blog( id,  uid,  content,  images,  time,  state,  fromBid)" +
					" values( ?,  ?,  ?,  ?,  ?,  ?,  ?)";
			//创建执行对象
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1,blog.getId());
			pstm.setInt(2,blog.getUid());
			pstm.setString(3,blog.getContent());
			pstm.setString(4,blog.getImages());
			pstm.setInt(5,blog.getState());
			pstm.setInt(6,blog.getFromBid());
			//执行
			return pstm.executeUpdate();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			//关闭
			closeAll(rs,pstm,conn);
		}
		return 0;
	}

	@Override
	public int update(Blog blog) {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			//获取连接对象
			conn = getConnection();
			//编写sql
//			String sql = "update  blog " +
//					" set uid=?,  content=?,  images=?,  time=?,  state=?,  fromBid=? " +
//					" where id=?";
			String sql = "update  blog " +
					" set  content=?" +
					" where id=?";
			//创建执行对象
			pstm = conn.prepareStatement(sql);
			pstm.setString(1,blog.getContent());
			pstm.setInt(2,blog.getId());
			//执行
			return pstm.executeUpdate();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			//关闭
			closeAll(rs,pstm,conn);
		}
		return 0;
	}

	@Override
	public int deleteById(Integer id) {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			//获取连接对象
			conn = getConnection();
			//编写sql
			String sql = "delete from blog where id=?";
			//创建执行对象
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1, id);
			//执行
			return pstm.executeUpdate();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			//关闭
			closeAll(rs,pstm,conn);
		}
		return 0;
	}

	@Override
	public List<Blog> findByParam(Integer uid, Integer state, String content) {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			//获取连接对象
			conn = getConnection();
			//编写sql
			String sql = "Select * from blog where 1=1 ";
			if(uid!=null){ //用户编号
				sql += " and uid="+uid;
			}
			if(state!=null && state!=-1){ //状态：原创 / 转发
				sql += " and state="+state;
			}
			if(content!=null && !content.trim().equals("")){
				sql += " and content like '%"+content+"%'";
			}
			//开始时间 - 结束时间
			String beginTime = null;//"2020-01-12 21:09:00";
			String endTime = "2020-11-12 21:09:00";

			if(beginTime!=null && endTime!=null){
				sql += " and time>'"+beginTime+"' and time<'"+endTime+"'";
			}else if(beginTime!=null){
				sql += " and time>'"+beginTime+"'";
			}else if(endTime!=null){
				sql += " and time<'"+endTime+"'";
			}

			sql += " order by id desc";

			System.out.println(sql);
			//创建执行对象
			pstm = conn.prepareStatement(sql);
			//执行
			rs = pstm.executeQuery();
			//处理
			List<Blog> list = new ArrayList<Blog>();
			while(rs.next()){
				Blog blog = new Blog(rs.getInt("id"),
						rs.getInt("uid"),
						rs.getString("content"),
						rs.getString("images"),
						rs.getString("time"),
						rs.getInt("state"),
						rs.getInt("fromBid"));
				list.add(blog); //将从数据库中获取的数据，循环添加到==》集合
			}
			return list;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			//关闭
			closeAll(rs,pstm,conn);
		}
		return null;
	}
}
